from a115.utils.listutils import compose
from random import randint
from decimal import Decimal

__all__ = ["Decimal", "Infix", "dcml", "pct", "randint", "mark_up", "mark_down"]

class Infix:
    def __init__(self, f):
        self.function = f
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __or__(self, other):
        return self.function(other)
    def __rlshift__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __rshift__(self, other):
        return self.function(other)
    def __call__(self, v1, v2):
        return self.function(v1, v2)

# convert anything to a Decimal
dcml = compose(Decimal, str) 

@Infix
def pct(p, x): 
    """ Calculate p percent from x """
    return dcml(p)*dcml(x)/dcml(100)

mark_up = lambda p,v: dcml(v) + dcml(p)*dcml(v)/dcml(100)
mark_down = lambda p,v: mark_up(dcml(-1)*dcml(p), v)
